package com.tmall.tmall.controller;

import com.tmall.tmall.pojo.Address;
import com.tmall.tmall.pojo.AddressRt;
import com.tmall.tmall.pojo.PageRt;
import com.tmall.tmall.pojo.Result;
import com.tmall.tmall.service.ShippingAddressService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shippingAddress")
public class ShippingAddressController {
    @Autowired
    private ShippingAddressService shippingAddressService;

    @PostMapping("/add")
    public Result add(@RequestBody Address address){
        shippingAddressService.insert(address);
        return Result.success();
    }
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,String tel,String address,Integer qwe){
        Integer uid=null;
        if(qwe!=null)uid= ThreadLocalUtils.getUid();
        PageRt list = shippingAddressService.page(pageNum,pageSize,name,tel,address,uid);
        return Result.success(list);
    }
    @PutMapping("/update")
    public Result update(@RequestBody Address address){
        shippingAddressService.update(address);
        return Result.success();
    }
    @DeleteMapping("/delBatch")
    public Result delete(@RequestBody List<Integer> ids){
        shippingAddressService.delete(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        List<AddressRt>list = shippingAddressService.getById(id);
        return Result.success(list);
    }
}
